//
//  SelectPhotoController.m
//  testApp
//
//  Created by iMac on 2018/2/9.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "SelectPhotoController.h"
#import "AssetModel.h"
#import "WDPhotoHandler.h"
#import "PhotoSelectCell.h"



#define screenWidth [UIScreen mainScreen].bounds.size.width
#define screenHeight [UIScreen mainScreen].bounds.size.height
CGFloat cellSpacing = 5;
NSInteger countInLine = 4;
CGFloat lineSpacing = 5;

@interface SelectPhotoController ()

@property (nonatomic, strong) NSArray *assetModels;
@property (nonatomic, strong) NSArray *selectedModels;
@property (nonatomic, assign) NSInteger selectedCount;

@end

@implementation SelectPhotoController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadAssets];
    self.selectedCount = 0;
    [self.collectionView registerClass:[PhotoSelectCell class] forCellWithReuseIdentifier:reuseIdentifier];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = lineSpacing;
    flowLayout.minimumInteritemSpacing = cellSpacing;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
    CGFloat cellWidth = (screenWidth - cellSpacing * (countInLine - 1)) / countInLine;
    flowLayout.itemSize = CGSizeMake(cellWidth, cellWidth);
    self.collectionView.bounces = false;
    self.collectionView.collectionViewLayout = flowLayout;
}




#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.assetModels.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PhotoSelectCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    AssetModel *model = self.assetModels[indexPath.item];
    cell.model = model;
    cell.selectedAsset = ^(AssetModel *model, UIButton *sender){
        NSMutableArray *marr = [NSMutableArray arrayWithArray:self.selectedModels];
        if (sender.isSelected) {
            sender.selected = !sender.selected;
            model.selected = sender.selected;
            [marr removeObject:model];
            self.selectedCount--;
        } else {
            if (self.selectedCount < self.allowSelectedCount) {
                sender.selected = !sender.selected;
                model.selected = sender.selected;
                [marr addObject:model];
                self.selectedCount++;
            }
        }
        
    };
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击cell");
}

#pragma mark <UICollectionViewDelegate>
- (void)loadAssets {
    NSArray * asseets = [[WDPhotoHandler defaultHandler] assetsFromCollection:self.collection];
    NSMutableArray *mArr = [NSMutableArray array];
    for (NSInteger i = 0; i < asseets.count; i++) {
        PHAsset *asset = asseets[i];
        AssetModel *model = [[AssetModel alloc] init];
        model.asset = asset;
        model.selected = false;
        [mArr addObject:model];
    }
    self.assetModels = mArr.copy;
}
- (void)setAssetModels:(NSArray *)assetModels {
    _assetModels = assetModels;
    [self.collectionView reloadData];
}



@end
